<template>
  <div class="all">
    <el-container>
      <!-- 顶部导航栏 -->
      <el-header>
      </el-header>
      <!-- 主内容 -->
      <el-main>
        <!-- 标题 -->
        <div class="header">
          <h1>惊喜一餐 · 餐厅管理系统</h1>
          <p>轻松管理您的餐厅信息，支持添加、修改、删除和搜索功能。</p>
        </div>

        <!-- 搜索框 -->
        <div class="search-container">
          <el-input
            v-model="searchQuery"
            placeholder="搜索餐厅名称"
            clearable
            class="search-box"
          />
          <el-button type="primary" @click="searchRestaurant">搜索</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </div>

        <!-- 添加餐厅表单 -->
        <div class="form-container">
          <h2>添加餐厅</h2>
          <el-form :model="newRestaurant" label-width="100px">
            <el-form-item label="餐厅名称">
              <el-input v-model="newRestaurant.name" placeholder="请输入餐厅名称" />
            </el-form-item>
            <el-form-item label="餐厅地址">
              <el-input v-model="newRestaurant.address" placeholder="请输入餐厅地址" />
            </el-form-item>
            <el-form-item>
              <el-button type="success" @click="addRestaurant">添加</el-button>
            </el-form-item>
          </el-form>
        </div>

        <!-- 餐厅列表 -->
        <div class="list-container">
          <h2>餐厅列表</h2>
          <el-table :data="filteredRestaurants" style="width: 100%">
            <el-table-column prop="name" label="餐厅名称" />
            <el-table-column prop="address" label="餐厅地址" />
            <el-table-column label="操作" width="200">
              <template #default="scope">
                <el-button size="mini" @click="editRestaurant(scope.row)">修改</el-button>
                <el-button size="mini" type="danger" @click="deleteRestaurant(scope.$index)">
                  删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 -->
          <el-pagination
            layout="prev, pager, next"
            :total="filteredRestaurants.length"
            :page-size="pageSize"
            v-model:current-page="currentPage"
            @current-change="changePage"
          />
        </div>

        <!-- 修改弹窗 -->
        <el-dialog title="修改餐厅信息" v-model="isEditDialogVisible">
          <el-form :model="editingRestaurant" label-width="100px">
            <el-form-item label="餐厅名称">
              <el-input v-model="editingRestaurant.name" />
            </el-form-item>
            <el-form-item label="餐厅地址">
              <el-input v-model="editingRestaurant.address" />
            </el-form-item>
          </el-form>
          <div class="dialog-footer">
            <el-button @click="isEditDialogVisible = false">取消</el-button>
            <el-button type="primary" @click="saveRestaurant">保存</el-button>
          </div>
        </el-dialog>
      </el-main>
    </el-container>

  </div>
</template>

<script>
import { ref } from 'vue'

export default {
  setup () {
    const activeMenu = ref('1')
    const searchQuery = ref('')
    const newRestaurant = ref({ name: '', address: '' })
    const restaurants = ref([])
    const filteredRestaurants = ref([])
    const pageSize = ref(10)
    const currentPage = ref(1)
    const isEditDialogVisible = ref(false)
    const editingRestaurant = ref({ name: '', address: '', index: null })

    const addRestaurant = () => {
      if (newRestaurant.value.name.trim() === '' || newRestaurant.value.address.trim() === '') {
        alert('请填写完整的餐厅信息')
        return
      }
      restaurants.value.push({ ...newRestaurant.value })
      resetForm()
      filterRestaurants()
    }

    const resetForm = () => {
      newRestaurant.value = { name: '', address: '' }
    }

    const deleteRestaurant = (index) => {
      restaurants.value.splice(index, 1)
      filterRestaurants()
    }

    const editRestaurant = (restaurant) => {
      editingRestaurant.value = { ...restaurant, index: restaurants.value.indexOf(restaurant) }
      isEditDialogVisible.value = true
    }

    const saveRestaurant = () => {
      if (editingRestaurant.value.name.trim() === '' || editingRestaurant.value.address.trim() === '') {
        alert('请填写完整的餐厅信息')
        return
      }
      restaurants.value[editingRestaurant.value.index] = { ...editingRestaurant.value }
      isEditDialogVisible.value = false
      filterRestaurants()
    }

    const searchRestaurant = () => {
      filterRestaurants()
    }

    const resetSearch = () => {
      searchQuery.value = ''
      filterRestaurants()
    }

    const filterRestaurants = () => {
      filteredRestaurants.value = restaurants.value.filter((restaurant) =>
        restaurant.name.toLowerCase().includes(searchQuery.value.toLowerCase())
      )
      currentPage.value = 1 // Reset to first page after search
    }

    const changePage = (newPage) => {
      currentPage.value = newPage
    }

    // Initialize filteredRestaurants with all restaurants
    filterRestaurants()

    return {
      activeMenu,
      searchQuery,
      newRestaurant,
      restaurants,
      filteredRestaurants,
      pageSize,
      currentPage,
      isEditDialogVisible,
      editingRestaurant,
      addRestaurant,
      resetForm,
      deleteRestaurant,
      editRestaurant,
      saveRestaurant,
      searchRestaurant,
      resetSearch,
      changePage
    }
  }
}
</script>

<style scoped>
.all{
  background: url("./img/img.png");
}
.navbar {
  background-color: #409eff;
  color: white;
  padding: 0 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.menu {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 60px;
}

.header {
  text-align: center;
  margin: 30px 0;
}

.header h1 {
  font-size: 2.5em;
  margin-bottom: 0.5em;
  color: azure;
}

.header p {
  font-size: 1em;
  color: azure;
}

.search-container {
  display: flex;
  justify-content: center;
  margin-bottom: 20px;
}

.search-box {
  width: 42%;
  margin-right: 10px;
}

.form-container {
  max-width: 800px;
  margin: 20px auto;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  background: aliceblue;
  opacity: 0.9; /* 50% 透明度 */
}

.form-container h2 {
  margin-bottom: 20px;
}

.list-container {
  max-width: 800px;
  margin: 20px auto;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  background: aliceblue;
  opacity: 0.9; /* 50% 透明度 */
}

.list-container h2 {
  margin-bottom: 20px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}

.el-pagination {
  margin-top: 20px;
  text-align: center;
}
</style>
